package com.sgcc.ln.pmos.e_ln_personalcenter.activity;


import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.base.MvpBaseActivity;
import com.sgcc.ln.pmos.common.utils.BigDecimalUtils;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.ln.pmos.e_ln_personalcenter.bean.EstimatePowerChangeBean;
import com.sgcc.ln.pmos.e_ln_personalcenter.contract.EstimatePowerChangeContract;
import com.sgcc.ln.pmos.e_ln_personalcenter.presenter.EstimatePowerChangePresenter;

/**
 * 预计用电量变更
 *
 * @author ChaoAJ
 * @describe
 * @time 2022/11/18 17:31
 */
public class EstimatePowerChangeActivity extends MvpBaseActivity<EstimatePowerChangePresenter> implements EstimatePowerChangeContract.View {

    private TitleBar titleBar;
    private TextView tvValue1;
    private TextView tvValue2;
    private TextView tvValue3;
    private TextView tvUpdateBtn;

    private double value1;
    private double value2;
    private double value3;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_estimate_power_change;
    }


    @Override
    protected EstimatePowerChangePresenter createPresenter() {
        return new EstimatePowerChangePresenter();
    }


    @Override
    protected void initViews() {
        super.initViews();
        titleBar = (TitleBar) findViewById(R.id.titleBar);
        tvValue1 = (TextView) findViewById(R.id.tvValue1);
        tvValue2 = (TextView) findViewById(R.id.tvValue2);
        tvValue3 = (TextView) findViewById(R.id.tvValue3);
        tvUpdateBtn = (TextView) findViewById(R.id.tvUpdateBtn);

        titleBar.setTitle("预计用电量变更");
        titleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        mPresenter.query();


    }


    @Override
    protected void initData() {
        super.initData();

        tvUpdateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = "是否选择使用最新结算电量：" + value3 + "MWh";
                DialogUtil.showConfirm(mContext, "提示", text, new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.update(value3 + "");
                    }
                });
            }
        });

    }

    @Override
    public void querySuccess(EstimatePowerChangeBean.DataDTO dataDTO) {
        String energyHis = UserManager.getUserBean().getEnergyHis();
        value1 = Double.parseDouble(BigDecimalUtils.onKeep3Decimal(energyHis));
        value2 = Double.parseDouble(BigDecimalUtils.onKeep3Decimal(dataDTO.getEnergyHisTemp()));
        value3 = Double.parseDouble(BigDecimalUtils.onKeep3Decimal(dataDTO.getEnergySettlementTemp()));

        tvValue1.setText(value1 + "MWh");
        tvValue2.setText(value2 + "MWh");
        tvValue3.setText(value3 + "MWh");

//        1.最新为0 不展示
//        2.0.8 <= 最新 / 上年 <= 1.2  不展示
//        3.当前 等于 最新 不展示

        if (value3 == 0 || value1 == value3 || (0.8f <= value3 / value2 && value3 / value2 <= 1.2f)) {
            tvUpdateBtn.setVisibility(View.GONE);
        } else {
            tvUpdateBtn.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void queryFailed() {

    }

    @Override
    public void updateSuccess() {
        ToastUtils.showLong("操作成功");
        UserManager.getUserBean().setEnergyHis(value3 + "");
        mPresenter.query();

    }

    @Override
    public void updateFailed() {

    }
}